<!--
 * Copyright ©
 * #  
 * @author: zw
 * @date: 2021-11-24 
 -->


<template>
  <basicheader :data="['修改密码']"></basicheader>
  <el-card>
    <el-row>
      <el-col :span="12">
        <el-form label-width="80px" status-icon :model="form" :rules="rules" ref="formNode">
          <el-form-item label="原始密码" prop="oldpassword">
            <el-input type="password" v-model="form.oldpassword"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newpassword">
            <el-input type="password" v-model="form.newpassword"></el-input>
          </el-form-item>
          <el-form-item label="重复密码" prop="repassword">
            <el-input type="password" v-model="form.repassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">确认修改</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </el-card>
</template>

<script lang='js'>
import basicheader from "@/components/basicHeader.vue"
import { defineComponent, reactive, toRefs, onMounted } from 'vue'
import { useInstance } from "@/common";

import { useMock, useMockPost, useMockget, useCommentList, usetextapi } from "@/api";
  function useCheckPassword(state) {
    const repassword = (rule, value, callback) => {
      if (value === state.form.newpassword) return callback();
      return callback(new Error("两次输入必须保持一致"));
    }
    return { repassword }
  }
  export default defineComponent({
    name: 'password',
    components: { basicheader },
    setup(props, {emit, slots}) {
      const { $message, $moment } = useInstance();
      const state = reactive({
        formNode: null,
        form: {
          oldpassword:'admin',
					newpassword:'123456',
					repassword:'123456'
        },
        rules: {
          oldpassword: [ { required:true, message:'请输入原始密码', trigger:'blur' } ],
					newpassword:[ { required:true, message:'请输入新密码', trigger:'blur' } ],
					repassword:[{ required:true, message:'请重复新密码', trigger:'blur', }, { validator:useCheckPassword(state).repassword, trigger:'blur' } ]
				}
      })

      const submit = async () => {
        state.formNode.validate(valid => {
          if (!valid) return $message.error('请先验证');
        });
        const { data } = await useMock();
        // const { data } = await useMockPost({name: '哎哎哎', price: 20, count: 21, img: ''});
        // const { data } = await useMockget(101);
        // const { data } = await useCommentList();
        // const { data } = await usetextapi({...state.form});
        console.log(data)
      }
      onMounted(() => {
        console.log(useInstance())
      })
     return {
       ...toRefs(state),
       submit
      }
     }
  })

</script>

<style lang='css' scoped></style>
